<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/GPL-3.0
 * @link https://www.gougucms.com
 */

declare(strict_types=1);

namespace app\lawyer\controller;

use app\base\BaseController;
use app\lawyer\model\Lawfirm as LawfirmList;
use app\lawyer\validate\LawfirmCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
use think\facade\Log;

class Lawfirm extends BaseController
{
    public function index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['title', 'like', '%' . $param['keywords'] . '%'];
            }

            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];

            // 按照id字段降序排序
            $lawfirmList = LawfirmList::where($where)
                ->order('id desc')
                ->paginate(['list_rows' => $rows])
                //->paginate($rows)
                // 对每个条目进行处理
                ->each(function ($item) {
                    // 省
                    if ($item->provinceid > 0) {
                        $item->province = Db::name('region')->where(['id' => $item->provinceid])->value('name');
                    } else {
                        $item->province = '-';
                    }
                    // 市
                    if ($item->cityid > 0) {
                        $item->city = Db::name('region')->where(['id' => $item->cityid])->value('name');
                    } else {
                        $item->city = '-';
                    }
                    // 区
                    if ($item->distid > 0) {
                        $item->dist = Db::name('region')->where(['id' => $item->distid])->value('name');
                    } else {
                        $item->dist = '-';
                    }
                });
            return table_assign(0, '', $lawfirmList);
        } else {
            return view();
        }
    }

    //添加|编辑律所
    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) { //处理提交数据
            if ($param['id'] > 0) { //编辑
                try {
                    validate(LawfirmCheck::class)->scene('edit')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                $param['update_time'] = time();

                Db::name('Lawfirm')->strict(false)->field(true)->update($param);
                add_log('edit', (int)$param['id'], $param);

            } else { //新增
                try {
                    validate(LawfirmCheck::class)->scene('add')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                $did = Db::name('Lawfirm')->strict(false)->field(true)->insertGetId($param);
                add_log('add', $did, $param);
            }
            return to_assign();
        } else { //数据初始化
            if (!empty($param['id']) && $param['id'] > 0) {
                $lawfirm = get_lawfirm();
                $detail = Db::name('Lawfirm')->where(['id' => $param['id']])->find();
                View::assign('detail', $detail);
                View::assign('lawfirm', $lawfirm);
                View::assign('id', $param['id']);
            } else {
                $detail = array();
                $detail['provinceid'] = '';
                $detail['cityid'] = '';
                $detail['distid'] = '';
                View::assign('detail', $detail);
                View::assign('lawfirm', '');
                View::assign('id', '0');
            }
            return view('add');
        }
    }

    //删除
    public function delete()
    {
        $id = get_params("id");

        $users = Db::name('Lawyer')->where([['firmid', '=', $id], ['status', '>=', 0]])->count();
        if ($users > 0) {
            return to_assign(1, "该律所下还有律师，无法删除");
        }
        if (Db::name('Lawfirm')->delete($id) !== false) {
            add_log('delete', (int)$id);
            return to_assign(0, "删除律所成功");
        } else {
            Log::error("删除律所失败，ID: $id");
            return to_assign(1, "删除失败");
        }
    }
}
